package org.jeecg.modules.relation.relachstandarddistribution.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.jeecg.modules.achachievementstandard.mapper.AchAchievementStandardMapper;
import org.jeecg.modules.achachievestandarditem.entity.AchAchieveStandardItem;
import org.jeecg.modules.achachievestandarditem.mapper.AchAchieveStandardItemMapper;
import org.jeecg.modules.achachievestandarditem.service.IAchAchieveStandardItemService;
import org.jeecg.modules.relation.relachstandarddistribution.entity.RelAchStandardDistribution;
import org.jeecg.modules.relation.relachstandarddistribution.mapper.RelAchStandardDistributionMapper;
import org.jeecg.modules.relation.relachstandarddistribution.service.IRelAchStandardDistributionService;
import org.jeecg.modules.relation.relachstandarddistribution.vo.RelAchStandardDistributionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.NoSuchElementException;

/**
 * @Description: rel_ach_standard_distribution
 * @author: baochangrong
 * @Date: 2022-03-17
 * @Version: V1.0
 */
@Service
public class RelAchStandardDistributionServiceImpl extends ServiceImpl<RelAchStandardDistributionMapper, RelAchStandardDistribution> implements IRelAchStandardDistributionService {

    @Autowired
    RelAchStandardDistributionMapper relAchStandardDistributionMapper;

    @Autowired
    AchAchieveStandardItemMapper standardItemMapper;

    @Autowired
    AchAchievementStandardMapper standardMapper;

    @Autowired
    @Lazy  //循环引用：AchAchieveStandardItemServiceImpl中引用了IRelAchStandardDistributionService
    IAchAchieveStandardItemService standardItemService;

    /**
     * voList 全部查询 分页
     */
    @Override
    public IPage<RelAchStandardDistributionVo> voList(Page<RelAchStandardDistributionVo> page, @Param("relAchStandardDistributionVo") RelAchStandardDistributionVo relAchStandardDistributionVo) {
        List<RelAchStandardDistributionVo> relAchStandardDistributionVoList = relAchStandardDistributionMapper.voList(page, relAchStandardDistributionVo);
        return page.setRecords(relAchStandardDistributionVoList);
    }

    /**
     * 根据试卷ID查询 对应的试卷-标准记录
     */
    @Override
    public List<RelAchStandardDistributionVo> getByPaperId(@Param("relAchStandardDistributionVo") RelAchStandardDistributionVo relAchStandardDistributionVo) {
        List<RelAchStandardDistributionVo> achStandardDistributionList = relAchStandardDistributionMapper.getByPaperId(relAchStandardDistributionVo);
        return achStandardDistributionList;
    }

    @Override
    public List<RelAchStandardDistribution> queryStandardIdByObjTypeId(String objId, String objType) {
        return relAchStandardDistributionMapper.queryStandardIdByObjTypeId(objId, objType);
    }

    @Override
    public List<RelAchStandardDistribution> queryAllByObjId(String examId) {
        QueryWrapper<RelAchStandardDistribution> queryWrapper = new QueryWrapper();
        queryWrapper.in("rel_obj_id", examId)
                .eq("paper_standard_effective", 1);
        return list(queryWrapper);
    }

    @Override
    public boolean cleanAllItemsAndRules(String examId) {
        List<RelAchStandardDistribution> distributionList = relAchStandardDistributionMapper.queryStandardIdByObjTypeId(examId, "93");
        try {
            if (distributionList.size() == 0) {
                return false;
            }
            String standardId = distributionList.get(0).getRelObjId();
            for (RelAchStandardDistribution item : distributionList) {
                QueryWrapper<AchAchieveStandardItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.likeRight("del_flag", 0);
                queryWrapper.likeRight("achievement_standard_id", item.getAchievementStandardId());
                standardItemMapper.delete(queryWrapper);
            }
            System.out.println(standardId);
            standardItemService.checkIsStandardItemEmpty(standardId);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return false;
    }

}
